<template>
  <!-- 基本信息 -->
  <a-card v-if="isCard" :bordered="false">
    <title-name :title="title"></title-name>
    <custom-form
      ref="ruleForm"
      :isValidate="isValidate"
      :form="form"
      :formList="formList"
      :rules="rules"
      :editModel="editModel == 0 ? 'V' : ''"
      @selectOrg="selectOrg"

    ></custom-form>
<!--    @change="-->
<!--    (name, e) => {-->
<!--    name === 'branchCompanyId' && $set(form, 'projectId', '')-->
<!--    $emit({ projectId: 'projectchange', branchCompanyId: 'companychange' }[name], e)-->
<!--    }-->
<!--    "-->
  </a-card>
  <div v-else class="mb20">
    <title-name :title="title"></title-name>
    <custom-form
      ref="ruleForm"
      :isValidate="isValidate"
      :form="form"
      :formList="formList"
      :rules="rules"
      :editModel="editModel == 0 ? 'V' : ''"
      @selectOrg="selectOrg"
    ></custom-form>
<!--    @change="-->
<!--    (name, e) => {-->
<!--    name === 'branchCompanyId' && $set(form, 'projectId', '')-->
<!--    $emit({ projectId: 'projectchange', branchCompanyId: 'companychange' }[name], e)-->
<!--    }-->
<!--    "-->
  </div>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
export default {
  name: 'BaseInfo',
  props: {
    // 表单
    form: Object,
    // 编辑状态
    editModel: [String, Number],
    // 是否校验
    isValidate: {
      type: Boolean,
      default: () => false
    },
    // 标题
    title: {
      type: String,
      default: () => '基本信息'
    },
    // 是否为卡片
    isCard: {
      type: Boolean,
      default: () => true
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: () => false
    },
    // 基础信息字段配置项
    replaceFieds: {
      type: Object,
      default: () => ({
        branchCompany: {
          id: 'branchCompanyId',
          name: 'branchCompanyName',
          label: '分公司名称'
        },
        project: {
          id: 'projectId',
          label: '项目名称',
          name: 'viewName'
        }
      })
    },
    //有些详情页不需要边框
    isNoBorder: {
      type: Boolean,
      defalut: false
    },
    isNeedAuth: {
      type: Boolean,
      defalut: true
    }
  },
  data() {
    return {
      rules: {
        branchCompanyId: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }]
      }
    }
  },
  computed: {
    // 表单列表
    formList() {
      return [
        {
          type: "orgTree",
          span: 16,
          keyName: this.replaceFieds.project?.id,
          label: "所属单位",
          disabled: this.disabled,
          viewName: this.replaceFieds.project?.name,
          isAutoSelectFirst: this.editModel == 2,
          isNeedAuth: this.isNeedAuth,
          checkStrictly: true,
          isNoBorder: this.isNoBorder,
          // statusSet:this.disabled ? [1,2,3,4,5,6]: [1,2,3]
          statusSet: [1,2,3,4,5,6]
        },
        // {
        //   type: SelectSubCompany,
        //   span: 8,
        //   keyName: this.replaceFieds.branchCompany?.id,
        //   label: this.replaceFieds.branchCompany?.label,
        //   disabled: this.disabled,
        //   viewName: this.replaceFieds.branchCompany?.name,
        //   attrs: {
        //     autoSelected: this.editModel == 2
        //   }
        // },
        // {
        //   type: SelectProject,
        //   span: this.editModel == 0 ? 16 : 8,
        //   keyName: this.replaceFieds.project?.id,
        //   label: this.replaceFieds.project?.label,
        //   disabled: this.disabled,
        //   viewName: this.replaceFieds.project?.name,
        //   attrs: {
        //     autoSelected: this.editModel == 2,
        //     keyValue: this.replaceFieds.branchCompany?.id,
        //     isClear: this.editModel == 2,
        //     isEdit: this.editModel != 2,
        //     form: this.form
        //   }
        // }
      ]
    }
  },
  methods: {
    selectOrg(val) {
      if(val && val.length > 1) {
        const projectId = val[val.length - 1].projectId
        const branchCompanyId = val[val.length - 2].orgId
        this.$emit("projectchange", projectId)
        this.$emit("companychange", branchCompanyId)
      }
      // this.$emit({ projectId: 'projectchange', branchCompanyId: 'companychange' }[name], e)
    },
    // 校验
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve(true)
          } else {
            this.$message.warning("请填写完所有必填项信息")
            reject('error submit!!')
          }
        })
      })
    }
  }
}
</script>
